package com.fare.taluo.vo.core.report;


import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import com.dayatang.domain.ValueObject;
import com.fare.taluo.GlobalsConst;
import com.fare.taluo.vo.core.PlaneInfo;
/**
 * 收益报表导出VO类
 * <p>查询出最低价与其它航空公司价格对比
 * <p>经由该类需要转换成指定的FTP上传CFR格式(需求中描述格式)
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-11 下午1:51:22
 * @version v1.0
 */
public class ReportCFRDataVO implements ValueObject{
	/**
	 * 统一定义每行中每个值之前分隔符
	 */
	public static final String line_field_separator = " ";

	/**
	 * 
	 */
	private static final long serialVersionUID = -7911494833150725151L;
	/**
	 * 记录ID
	 */
	private String recordID;
	/**
	 * 数据提取时间
	 */
	private Date extractDateTime = new Date();	
	/**
	 * 客舱类型，取值1 (首舱First), 2 (Business商务舱), 3 (Economy经济舱), 为0时是没有舱位
	 * 暂时没有取到值时，默认为3经济舱
	 */
	private int cabin = 3;
	/**
	 * 出发机场代码
	 */
	private String airportOrigin;  
	/**
	 * 目的机场代码
	 */
	private String airportDestination;
	
	/**
	 * 飞行时间
	 */
	private long durationTime;
	
	/**
	 * 最低价
	 */
	private double lowestNetNetFareAvailable;
	/**
	 * 总费用最低价，包括税收和佣金
	 */
	private double lowestTotalFareAvailable;
	/**
	 * 承运人代码
	 */
	private String carrierCode;
	/**
	 * 航班号
	 */
	private String flightNumber; 
	/**
	 * 启程时间
	 */
	private Date DepartureDateTime;
	/**
	 * 到达时间
	 */
	private Date archiveDateTime;
	
	public ReportCFRDataVO(){}
	
	public ReportCFRDataVO(PlaneInfo plane) throws ParseException{
		this.recordID = plane.getId();
		this.airportDestination = plane.getToCity();
		this.airportOrigin = plane.getFromCity();
		//if(StringUtils.isNotEmpty(plane.getCabinInfos()))
		//this.cabin = //抓取回来的没有该数据
		this.carrierCode = plane.getCarrierKey();
		this.DepartureDateTime = plane.getFlightStartTime();
		this.archiveDateTime = plane.getFlightEndTime();
		
		//TODO 是否就是到达时间减启程时间？
		if(StringUtils.isNotEmpty(plane.getFlightBatchStartTime()) && 
				StringUtils.isNotEmpty(plane.getFlightBatchEndTime())){
			this.durationTime = this.archiveDateTime.getTime() - this.DepartureDateTime.getTime();
		}
		this.flightNumber = plane.getFlightBatch();
		
		this.lowestNetNetFareAvailable = StringUtils.isNotEmpty(plane.getLowprPrice()) && NumberUtils.isNumber(plane.getLowprPrice())
				? Double.valueOf(plane.getLowprPrice()) : -1;
		//TODO 没有这个值，暂时取与最低价一样的值
		this.lowestTotalFareAvailable = this.lowestNetNetFareAvailable;
	}
	
	public String getRecordID() {
		return recordID;
	}
	public void setRecordID(String recordID) {
		this.recordID = recordID;
	}
	public Date getExtractDateTime() {
		return extractDateTime;
	}
	public void setExtractDateTime(Date extractDateTime) {
		this.extractDateTime = extractDateTime;
	}
	public int getCabin() {
		return cabin;
	}
	public void setCabin(int cabin) {
		this.cabin = cabin;
	}
	public String getAirportOrigin() {
		return airportOrigin;
	}
	public void setAirportOrigin(String airportOrigin) {
		this.airportOrigin = airportOrigin;
	}
	public String getAirportDestination() {
		return airportDestination;
	}
	public void setAirportDestination(String airportDestination) {
		this.airportDestination = airportDestination;
	}
	public long getDurationTime() {
		return durationTime;
	}
	public void setDurationTime(long durationTime) {
		this.durationTime = durationTime;
	}
	public double getLowestNetNetFareAvailable() {
		return lowestNetNetFareAvailable;
	}
	public void setLowestNetNetFareAvailable(double lowestNetNetFareAvailable) {
		this.lowestNetNetFareAvailable = lowestNetNetFareAvailable;
	}
	public double getLowestTotalFareAvailable() {
		return lowestTotalFareAvailable;
	}
	public void setLowestTotalFareAvailable(double lowestTotalFareAvailable) {
		this.lowestTotalFareAvailable = lowestTotalFareAvailable;
	}
	public String getCarrierCode() {
		return carrierCode;
	}
	public void setCarrierCode(String carrierCode) {
		this.carrierCode = carrierCode;
	}
	public String getFlightNumber() {
		if(StringUtils.isNotBlank(flightNumber)){
			//苟且一点，直接替换值
			if(flightNumber.indexOf(this.getCarrierCode()) > -1){
				flightNumber = flightNumber.replace(this.getCarrierCode(), "");
			}
			if(flightNumber.indexOf(GlobalsConst.SEPARATOR_VERTICAL) > -1){
				flightNumber = flightNumber.substring(0, flightNumber.indexOf(GlobalsConst.SEPARATOR_VERTICAL));
			}
		}
		return flightNumber;
	}
	public void setFlightNumber(String flightNumber) {
		this.flightNumber = flightNumber;
	}
	public Date getDepartureDateTime() {
		return DepartureDateTime;
	}
	public void setDepartureDateTime(Date departureDateTime) {
		DepartureDateTime = departureDateTime;
	}

	public Date getArchiveDateTime() {
		return archiveDateTime;
	}

	public void setArchiveDateTime(Date archiveDateTime) {
		this.archiveDateTime = archiveDateTime;
	}
	/**
	 * 字段DurationTime是毫秒计，进行时分格式化
	 * @return 格式化时分，如：0515代表5小时15分
	 */
	private String getDurationFormatTime(){
		if(this.getDurationTime() > 0){ //没有飞行时长
			return com.fare.taluo.util.DateUtils.formatHourMinute(this.getDurationTime());
		}else{
			return "0000";
		}
	}

	@Override
	public String toString() {
		//CFR 20090711 2251 1 JFK LHR 0630 2814 3291 CO 0012 20090718 1710
		StringBuilder builder = new StringBuilder();
		builder.append(this.getRecordID()).append(line_field_separator);
		builder.append(DateFormatUtils.format(this.getExtractDateTime(), "yyyyMMdd")).append(line_field_separator);
		builder.append(DateFormatUtils.format(this.getExtractDateTime(), "HHmm")).append(line_field_separator);
		builder.append(this.getCabin()).append(line_field_separator);
		builder.append(this.getAirportOrigin()).append(line_field_separator);
		builder.append(this.getAirportDestination()).append(line_field_separator);
		//飞行时间要按时分格式化
		builder.append(this.getDurationFormatTime()).append(line_field_separator);
		builder.append(com.fare.taluo.util.value.NumberUtils
				.div(this.getLowestNetNetFareAvailable(), 1d, 0)).append(line_field_separator);
		builder.append(com.fare.taluo.util.value.NumberUtils
				.div(this.getLowestTotalFareAvailable(), 1d, 0)).append(line_field_separator);
		builder.append(this.getCarrierCode()).append(line_field_separator);
		if(this.getFlightNumber().indexOf(this.getCarrierCode()) > -1){
			
		}
		builder.append(this.getFlightNumber()).append(line_field_separator);
		builder.append(DateFormatUtils.format(this.getDepartureDateTime(), "yyyyMMdd")).append(line_field_separator);
		builder.append(DateFormatUtils.format(this.getDepartureDateTime(), "HHmm")).append(line_field_separator);
		//builder.append("\n");
		return builder.toString();
	}  
	
	
}
